package com.chushouya.order.service.common.impl;

import com.chushouya.common.support.Contexts;
import com.general.framework.core.lang.Dates;
import com.chushouya.order.dao.entity.OrderEntity;
import com.chushouya.order.dao.entity.OrderLogEntity;
import com.chushouya.order.dao.repository.OrderLogRepository;
import com.chushouya.order.dao.repository.OrderRepository;
import com.chushouya.order.dto.admin.orderlog.OrderLogDTO;
import com.chushouya.order.dto.admin.order.OrderLogQuery;
import com.chushouya.order.service.common.OrderLogService;
import com.general.framework.core.lang.Beans;
import com.general.framework.core.lang.Strings;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
public class OrderLogServiceImpl implements OrderLogService {

    @Resource
    private OrderLogRepository orderLogRepository;

    @Resource
    private OrderRepository orderRepository;



    @Override
    public void addOrderActionLog(Long orderId, Integer actionStatus, String actionNote) {
        String actionUser = Strings.isEmpty(Contexts.getUserContext().getUserName()) ? "系统" : Strings.format("{}",Contexts.getUserContext().getUserName());
        OrderLogEntity orderActionLogEntity = new OrderLogEntity();
        orderActionLogEntity.setOrderId(orderId);
        orderActionLogEntity.setActionUser(actionUser);
        orderActionLogEntity.setOrderStatus(actionStatus);
        orderActionLogEntity.setActionStatus(actionStatus);
        orderActionLogEntity.setActionNote(actionNote);
        orderActionLogEntity.setCreateTime(Dates.getTimeNow());
        orderActionLogEntity.setUpdateTime(Dates.getTimeNow());
        orderLogRepository.insert(orderActionLogEntity);
        //更新订单最后操作记录
        OrderEntity orderUpdate = new OrderEntity();
        orderUpdate.setOrderId(orderId);
        orderUpdate.setLastAction(actionNote);
        orderRepository.updateByPrimaryKeySelective(orderUpdate);
    }

    @Override
    public void addOrderActionLog(Long orderId, String actionUser, Integer actionStatus, String actionNote) {
        OrderLogEntity orderActionLogEntity = new OrderLogEntity();
        orderActionLogEntity.setOrderId(orderId);
        orderActionLogEntity.setActionUser(Strings.isEmpty(actionUser) ? "系统" : actionUser);
        orderActionLogEntity.setOrderStatus(actionStatus);
        orderActionLogEntity.setActionStatus(actionStatus);
        orderActionLogEntity.setActionNote(actionNote);
        orderActionLogEntity.setCreateTime(Dates.getTimeNow());
        orderActionLogEntity.setUpdateTime(Dates.getTimeNow());
        orderLogRepository.insert(orderActionLogEntity);
        //更新订单最后操作记录
        OrderEntity orderUpdate = new OrderEntity();
        orderUpdate.setOrderId(orderId);
        orderUpdate.setLastAction(actionNote);
        orderRepository.updateByPrimaryKeySelective(orderUpdate);
    }

    @Override
    public List<OrderLogDTO> orderLogList(Long orderId) {
        OrderLogQuery orderLogQuery = new OrderLogQuery();
        orderLogQuery.setOrderId(orderId);
        return Beans.copyList(orderLogRepository.selectList(orderLogQuery), OrderLogDTO.class);
    }
}
